package indi.lqzhi.basic.ui.basic.action;

import android.util.Log;

import java.lang.ref.WeakReference;

/**
 * @author FaZhi
 * @date 2021年02月20日 21时58分
 * @description This is LogAction Interface.
 */
public interface LogAction {

    int V = Log.VERBOSE;
    int D = Log.DEBUG;
    int I = Log.INFO;
    int W = Log.WARN;
    int E = Log.ERROR;

    default boolean isDebug(){
        return true;
    }

    default void  v(String msg){
        log(V,msg,null);
    }

    default void  v(String msg, Throwable tr){
        log(V,msg,tr);
    }

    default void  d(String msg){
        log(D,msg,null);
    }

    default void  d(String msg, Throwable tr){
        log(D,msg,tr);
    }

    default void  i(String msg){
        log(I,msg,null);
    }

    default void  i(String msg, Throwable tr){
        log(I,msg,tr);
    }

    default void  w(String msg){
        log(W,msg,null);
    }

    default void  w(String msg, Throwable tr){
        log(W,msg,tr);
    }

    default void  e(String msg){
        log(E,msg,null);
    }

    default void  e(String msg, Throwable tr){
        log(E,msg,tr);
    }

    default void log(final int type,
                     String msg,
                     Throwable tr) {
        if (!isDebug()){
            return;
        }
        StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
        if (traceElements.length < 4){
            //信息不够,直接处理
            if (tr != null){
                Log.e("LogAction Notice",msg,tr);
            }else {
                Log.e("LogAction Notice",msg);
            }
            return;
        }
        WeakReference<StackTraceElement[]> weakTraceElements = new WeakReference<>(traceElements);
        traceElements = null;
        //获取类名,方法名,当前执行的行数
        final String className = weakTraceElements.get() != null ? weakTraceElements.get()[4].getClassName() : "LogAction";
        final String methodName = weakTraceElements.get() != null ? weakTraceElements.get()[4].getMethodName() : "log";
        final int lineNumber = weakTraceElements.get() != null ? weakTraceElements.get()[4].getLineNumber() : -1;
        //封装一下tag
        if (tr != null){
            msg = methodName + "-> #"+lineNumber+" :"+msg+"\n error:"+tr.getMessage();
        }else {
            msg = methodName + "-> #"+lineNumber+" :"+msg;
        }
        switch (type){
            case V:
                if (tr != null) {
                    Log.v(className, msg);
                } else {
                    Log.v(className, msg,tr);
                }
                break;
            case D:
                if (tr != null) {
                    Log.d(className, msg);
                } else {
                    Log.d(className, msg,tr);
                }
                break;
            case I:
                if (tr != null) {
                    Log.i(className, msg);
                } else {
                    Log.i(className, msg,tr);
                }
                break;
            case W:
                if (tr != null) {
                    Log.w(className, msg);
                } else {
                    Log.w(className, msg,tr);
                }
                break;
            case E:
                if (tr != null) {
                    Log.e(className, msg);
                } else {
                    Log.e(className, msg,tr);
                }
                break;
            default:
                Log.wtf(className,"What the Fack?");
        }
    }
}
